package com.ruyuan.eshop.market.service.impl;

import com.ruyuan.eshop.common.utils.ParamCheckUtil;
import com.ruyuan.eshop.market.dao.CouponConfigDAO;
import com.ruyuan.eshop.market.dao.CouponDAO;
import com.ruyuan.eshop.market.domain.dto.UserCouponDTO;
import com.ruyuan.eshop.market.domain.entity.CouponConfigDO;
import com.ruyuan.eshop.market.domain.entity.CouponDO;
import com.ruyuan.eshop.market.domain.query.UserCouponQuery;
import com.ruyuan.eshop.market.domain.request.LockUserCouponRequest;
import com.ruyuan.eshop.market.domain.request.ReleaseUserCouponRequest;
import com.ruyuan.eshop.market.enums.CouponUsedStatusEnum;
import com.ruyuan.eshop.market.exception.MarketBizException;
import com.ruyuan.eshop.market.exception.MarketErrorCodeEnum;
import com.ruyuan.eshop.market.service.CouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author lxg
 * @Description:
 * @date 2022/3/2621:13
 */
@Service
public class CouponServiceImpl implements CouponService {
    @Autowired
    private CouponDAO couponDAO;
    @Autowired
    private CouponConfigDAO couponConfigDAO;
    @Override
    public Boolean lockUserCoupon(LockUserCouponRequest lockUserCouponRequest) {
        checkLockUserCouponRequest(lockUserCouponRequest);
        String userId=lockUserCouponRequest.getUserId();
        String couponId=lockUserCouponRequest.getCouponId();
        CouponDO couponDO = couponDAO.getUserCoupon(userId, couponId);
        if (couponDO == null){
            throw new MarketBizException(MarketErrorCodeEnum.USER_COUPON_IS_NULL);
        }
        //判断优惠卷是否使用
        if (CouponUsedStatusEnum.USED.getCode().equals(couponDO.getUsed())){
            throw new MarketBizException(MarketErrorCodeEnum.USER_COUPON_IS_USED);
        }
        couponDO.setUsed(CouponUsedStatusEnum.USED.getCode());
        couponDO.setUsedTime(new Date());
        couponDAO.updateById(couponDO);
        return true;
    }

    private void checkLockUserCouponRequest(LockUserCouponRequest lockUserCouponRequest) {
        String userId = lockUserCouponRequest.getUserId();
        String couponId = lockUserCouponRequest.getCouponId();
        ParamCheckUtil.checkStringNonEmpty(userId);
        ParamCheckUtil.checkStringNonEmpty(couponId);
    }

    @Override
    public UserCouponDTO getUserCoupon(UserCouponQuery userCouponQuery) {
        //入参检查
        String userId=userCouponQuery.getUserId();
        String couponId=userCouponQuery.getCouponId();
        ParamCheckUtil.checkStringNonEmpty(userId);
        ParamCheckUtil.checkStringNonEmpty(couponId);
        // 判断用户优惠券是否存在
        CouponDO userCoupon = couponDAO.getUserCoupon(userId, couponId);
        if (userCoupon==null){
            throw new MarketBizException(MarketErrorCodeEnum.USER_COUPON_IS_NULL);
        }
        String couponConfigId = userCoupon.getCouponConfigId();
        // 判断优惠券活动配置信息是否存在
        CouponConfigDO couponConfigDO = couponConfigDAO.getByCouponConfigId(couponConfigId);
        if (couponConfigDO==null){
            throw new MarketBizException(MarketErrorCodeEnum.USER_COUPON_CONFIG_IS_NULL);
        }

        UserCouponDTO userCouponDTO = new UserCouponDTO();
        userCouponDTO.setUserId(userId);
        userCouponDTO.setCouponConfigId(couponConfigId);
        userCouponDTO.setCouponId(couponId);
        userCouponDTO.setName(couponConfigDO.getName());
        userCouponDTO.setType(couponConfigDO.getType());
        userCouponDTO.setAmount(couponConfigDO.getAmount());
        userCouponDTO.setConditionAmount(couponConfigDO.getConditionAmount());
        userCouponDTO.setValidStartTime(couponConfigDO.getValidStartTime());
        userCouponDTO.setValidEndTime(couponConfigDO.getValidEndTime());
        return userCouponDTO;
    }

    @Override
    public Boolean releaseUserCoupon(ReleaseUserCouponRequest releaseUserCouponRequest) {
        String userId = releaseUserCouponRequest.getUserId();
        String couponId = releaseUserCouponRequest.getCouponId();
        CouponDO couponAchieve = couponDAO.getUserCoupon(userId, couponId);
        couponAchieve.setUsed(CouponUsedStatusEnum.UN_USED.getCode());
        couponAchieve.setUsedTime(null);
        couponDAO.updateById(couponAchieve);
        return true;
    }
}
